/******************************************************************
 *
 *    Java Lib For Android, Powered By GuangZhou WLB.
 *
 *    Copyright (c) 2001-2014 Digital Telemedia Co.,Ltd
 *    http://www.d-telemedia.com/
 *
 *    Package:     cn.myapp.mobile.owner.activity
 *
 *    Filename:    ActivityPhotoViewList.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2001-2014
 *
 *    Company:     Digital Telemedia Co.,Ltd
 *
 *    @author: WLB
 *
 *    @version: 1.0.0
 *
 *    Create at:   2016年6月1日 下午1:56:05
 *
 *    Revision:
 *
 *    2016年6月1日 下午1:56:05
 *        - first revision
 *
 *****************************************************************/
package com.eboss2.sbs.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.eboss2.sbs.R;
import com.eboss2.sbs.fragment.FragmentPhoto;
import com.eboss2.sbs.model.PhotoVO;
import com.tool.utils.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author WLB
 * @ClassName ActivityPhotoviewList
 * @category 查看多张图片
 * @Date 2016年6月1日 下午1:56:05
 * @Description 查看多张图片 ActivityPhotoviewList  查看单张图片 ActivityPhotoview
 */
public class ActivityPhotoviewList extends FragmentActivity implements OnClickListener {
    private String title = "图片预览";
    private List<PhotoVO> paths = new ArrayList<PhotoVO>();
    private ViewPager viewpager;
    private List<FragmentPhoto> fragmentlist = null;
    private TextView curr_select, remark;

    private int position;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photolist);

        if (getIntent().getExtras() == null) {
            finish();
        }

        paths = (List<PhotoVO>) getIntent().getSerializableExtra("paths");
        if (paths == null) {
            Toast.makeText(getApplicationContext(), "缺少必要参数", Toast.LENGTH_SHORT).show();
            finish();
        }
        position = getIntent().getIntExtra("position", 0);

        initTilte();
        initView();
        initViewpager();

    }

    private void initView() {
        curr_select = (TextView) findViewById(R.id.curr_select);
        remark = (TextView) findViewById(R.id.remark);

        int index = position + 1;
        curr_select.setText("(" + index + "/" + paths.size() + ")");
        remark.setText(paths.get(position).getRemark());

    }

    /**
     * @Description (TODO这里用一句话描述这个方法的作用)
     */
    private void initViewpager() {
        fragmentlist = new ArrayList<FragmentPhoto>();
        for (int i = 0; i < paths.size(); i++) {
            String path = "";
            if (StringUtils.isBlank(paths.get(i).getImgPath())) {
                path = paths.get(i).getWebimgPath();
            } else {
                path = paths.get(i).getImgPath();
            }
            FragmentPhoto fragment = new FragmentPhoto();
            Bundle bundle = new Bundle();
            bundle.putString(FragmentPhoto.KEY_PATH, path);
            fragment.setArguments(bundle);
            fragmentlist.add(fragment);
        }

        viewpager = (ViewPager) findViewById(R.id.viewpager);
        viewpager.setAdapter(new MyViewpagerAdapter(getSupportFragmentManager()));
        viewpager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                int i = arg0 + 1;
                curr_select.setText("(" + i + "/" + paths.size() + ")");
                remark.setText(paths.get(arg0).getRemark());
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        //当为第一个界面是时候不会去调用setOnPageChangeListener
        viewpager.setCurrentItem(position);

    }

    /**
     * @Description (TODO这里用一句话描述这个方法的作用)
     */
    private void initTilte() {
        TextView head = (TextView) findViewById(R.id.tv_header);
        head.setText(title);

        Button back = (Button) findViewById(R.id.backBtn);
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private class MyViewpagerAdapter extends FragmentPagerAdapter {

        public MyViewpagerAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int arg0) {
            return fragmentlist.get(arg0);
        }

        @Override
        public int getCount() {
            return fragmentlist != null ? fragmentlist.size() : 0;
        }

    }

    /* (非 Javadoc)
     * Description:
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}
